package security.configuration;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import network.KrakenClient;
import network.KrakenClientMsg;

import org.hibernate.Session;
import org.hibernate.Transaction;

import tools.ConfParser;
import tools.FileUtils;

import bdd.standalone.bean.Encryption;
import bdd.standalone.bean.GlobalServer;
import bdd.standalone.bean.SiteServer;
import bdd.standalone.hibernate.HibernateUtil;

public class SecurityConfiguration {

	private Session session;
	private String pathFolderOrigin;
	private String separator;
	private List <GlobalServer> servers;
	private List<SiteServer> sites;
	private List<Encryption> encryptions;
	
	public SecurityConfiguration(){
		this.session = HibernateUtil.getSession();
		separator = System.getProperty("file.separator");
		ConfParser parser = new ConfParser();
		try {
			parser.parse(new File("C:\\Users\\M1kraken\\Desktop\\vsm\\conf\\kraken.conf"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		pathFolderOrigin= parser.getValue("WEB_PATH_CLASSES");
	}
	
	@SuppressWarnings("unchecked")
	public void getAllGlobalServer(){ this.servers = this.session.createCriteria(GlobalServer.class).list();	}
	@SuppressWarnings("unchecked")
	public void getAllSiteServer(){ this.sites = this.session.createCriteria(SiteServer.class).list();	}
	@SuppressWarnings("unchecked")
	public void getAllEncryption(){ this.encryptions = this.session.createCriteria(Encryption.class).list();	}
	
	public List<GlobalServer> getServers(){	return this.servers;	}
	public List<SiteServer> getSites(){	return this.sites;	}
	public List<Encryption> getEncryptions(){ return this.encryptions;	}
	public String getPathFolderOrigin(){ return this.pathFolderOrigin;	}
	public String getSeparator(){ return this.separator;	}
	
	
	
	//test
	public static void main(String[] args) {
		SecurityConfiguration config = new SecurityConfiguration();
		String answer = "";
		File[] files = FileUtils.listFiles(config.getPathFolderOrigin());
		Encryption encryption;
		Transaction tx = null;
		
		config.getAllGlobalServer();
		config.getAllSiteServer();
		config.getAllEncryption();
		
		//pour chaque serveurs
		for(GlobalServer server : config.getServers()){
			if(server.getGlobserv_name().equalsIgnoreCase("GL0")){
				System.out.println("Server "+server.getGlobserv_name());
				//pour chaques sites
				//for(SiteServer site : config.getSites()){
				System.out.println(server.getSiteServers().size());
				for(SiteServer site : server.getSiteServers()){
					if(site.getSiteserv_name().equalsIgnoreCase("LO0")){
						//test sur serveur :
						String ip = server.getGlobserv_ip();
						ArrayList<String> msg = new ArrayList<String>();
						msg.add(KrakenClientMsg.KRAKEN_CLIENT.toString());
						msg.add(KrakenClientMsg.ENCRYPTION_DISTRIBUTION.makeCommand(" GLOBAL", " "+config.getPathFolderOrigin()));
						msg.add(KrakenClientMsg.END_CLIENT.toString());
								
						KrakenClient krakenClient = new KrakenClient(msg,ip);
						krakenClient.connect();
						krakenClient.communicate();
								
						answer = krakenClient.getFinalAnswer();
						if(answer.equalsIgnoreCase("END_SERVER"))	System.out.println("Distribution des classes réussie");
						
					}
				}
				
			}
		}
		
		//si opérations bien passées, on introduit les algos dans la BDD
		if(answer.equalsIgnoreCase("END_SERVER")){
			tx = config.session.beginTransaction();
			for(Encryption encrypt : config.getEncryptions()) {
				for(SiteServer site : encrypt.getSites()) {
					site.setEncryption(null);
					config.session.save(site);
				}
				config.session.delete(encrypt);
			}
			
			tx.commit();
			tx = config.session.beginTransaction();
			String tmp[];
			
			for(File file : files){
				tmp = file.getName().split("\\.");
				//System.out.println(file.getName()+" "+tmp.length);
				//System.out.println("Debug tmp: "+tmp[0]);
				encryption = new Encryption(tmp[0]);
				//encryption = new Encryption(file.getName());
				config.session.save(encryption);
			}
			tx.commit();
		}
		else{
			System.out.println("Erreur ENCRYPTION_DISTRIBUTION failed");
		}
	}

}
